package lvbu.wang.lvbuoperator.ui.error;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lvbu.wang.lvbuoperator.R;
import lvbu.wang.lvbuoperator.adapter.ErrorListAdapter;
import lvbu.wang.lvbuoperator.bean.ErrorBean;
import lvbu.wang.lvbuoperator.constants.RouteConstant;
import lvbu.wang.lvbuoperator.core.BaseActivity;
import lvbu.wang.lvbuoperator.http.IotHttpHelp;
import lvbu.wang.lvbuoperator.manager.ThreadPool;

/**
 * Created by DaiQing.Zha on 2016/12/13 0013.
 */
public class IotErrorActivity extends BaseActivity {

    private ListView mLstError;   //错误列表
    private Button mBtnRefresh; //刷新按钮
    private long mMotorId;    //motorId
    private ErrorListAdapter mErrorListAdapter;
    private List<ErrorBean> mErrorBeanList;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_iot_see_error);
    }
    @Override
    protected void init() {
        mLstError = (ListView) findViewById(R.id.lst_error);
        mBtnRefresh = (Button) findViewById(R.id.btn_refresh);
        mErrorBeanList = new ArrayList<>();
        Intent intent = getIntent();
        Bundle bundle = intent.getBundleExtra("bundle");
        if (bundle != null){
            mMotorId = bundle.getLong("motorId",0);
        }
        mErrorListAdapter = new ErrorListAdapter(this, mErrorBeanList);
        mLstError.setAdapter(mErrorListAdapter);
        getErrorInfo();
        mBtnRefresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mErrorBeanList.clear();
                mErrorListAdapter.notifyDataSetChanged();
                getErrorInfo();
            }
        });
        getSupportActionBar().setTitle("错误详情");
    }
    /**
     * 获取错误信息
     */
    private void getErrorInfo(){
        ThreadPool.getInstance().addTask(new Runnable() {
            @Override
            public void run() {
                Map<String, Object> queryStrings = new HashMap<String, Object>();
                queryStrings.put("motorId", mMotorId);
                String source = IotHttpHelp.getData(RouteConstant.GET_LIST_JSON_MOTOR_MOTOR_FAULT_SELECT_BY_MOTOR_ID, queryStrings, true, IotErrorActivity.this);
                List<ErrorBean> errorBeanListT = new Gson().fromJson(source, new TypeToken<List<ErrorBean>>() {}.getType());
                for (int i = 0;i < errorBeanListT.size() && i < 5;i ++){    //如果有很多条错误，我们最多获取前面5条
                    mErrorBeanList.add(errorBeanListT.get(i));
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mErrorListAdapter.notifyDataSetChanged();
                    }
                });
            }
        });
    }
}
